package redarrow.wb.business;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import redarrow.wb.dao.UrlDao;
import redarrow.wb.dao.UserDao;
import redarrow.wb.output.AddUrlOutput;
import redarrow.wb.output.GenUserOutput;
import redarrow.wb.output.ListUserWebOutput;
import redarrow.wb.util.CommonError;
import redarrow.wb.util.DateUtil;
import redarrow.wb.util.IConstants;

public class WebBookFunction {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ListUserWebOutput test = new ListUserWebOutput();
		test = getListUserWeb("hongson");
		System.out.println(test.getCode());
		System.out.println(test.getMessage());
	}

	public static AddUrlOutput addUrl(String username, String url_address) {
		AddUrlOutput output = new AddUrlOutput();
		// Add Url to Database
		String now = DateUtil.getCurrentTimeStamp();
		int errorType = UrlDao.addUrl(username, url_address, now, "1");
		String message = CommonError.getErrorMessage(errorType);
		output.setCode(errorType);
		output.setMessage(message);
		return output;
	}

	public static GenUserOutput createUser(String username, String password,
			String userGenDate, String status) {
		GenUserOutput output = new GenUserOutput();
		// Check user has exist or not;
		Boolean foo;
		int errorType = IConstants.SUCCESS;
		String message = CommonError.getErrorMessage(errorType);
		try {
			foo = UserDao.checkUserExist(username);
			if (!foo) {
				// Add user to database
				int errorCode = UserDao.InsertUser(username, password,
						userGenDate, status);
				output.setMessage(CommonError.getErrorMessage(errorCode));
				output.setCode(errorCode);
				output.setUsername(username);
			}

			else {
				errorType = IConstants.USER_EXIST;
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			errorType = IConstants.CLASS_NOT_FOUND;

		} catch (IOException e) {
			// TODO Auto-generated catch block
			errorType = IConstants.IOException;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			errorType = IConstants.SQLException;

		}

		message = CommonError.getErrorMessage(errorType);
		output.setCode(errorType);
		output.setMessage(message);
		output.setUsername(username);
		return output;
	}

	public static ListUserWebOutput getListUserWeb(String username) {
		ListUserWebOutput listWebUser = new ListUserWebOutput();
		// getList from database
		int errorType = 0;
		String[] urlList = null;
		try {
			urlList = UrlDao.getUserUrls(username);
			errorType = IConstants.SUCCESS;
			listWebUser.setListUrl(urlList);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			errorType = IConstants.CLASS_NOT_FOUND;
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			errorType = IConstants.SQLException;
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			errorType = IConstants.IOException;
			e.printStackTrace();
		}
		
		listWebUser.setCode(errorType);
		listWebUser.setMessage(CommonError.getErrorMessage(errorType));		
		return listWebUser;
	}

	public static boolean checkUserExist(String username) {
		// TODO Auto-generated method stub
		try {
			return UserDao.checkUserExist(username);
		} catch (ClassNotFoundException | IOException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}		
	}

}
